<?php

require_once('../includes/DB.class.php');
require_once('../includes/SmartyManager.class.php');
require_once('../includes/exceptions/AppExceptions.class.php');
require_once('../includes/Verifier.class.php');

$smarty = SmartyManager::getSmarty();

try{

$_GET['keyname'] = trim($_GET['keyname']);

$smarty->assign('keyname', $_GET['keyname']);
$smarty->assign('stype', $_GET['stype']);


if($_GET['stype'] == 'filename'){
	
	if(empty($_GET['keyname'])){
		//如果在按照文件名查询的时候没有输入关键字，结果就会按照上传时间列出所有文件。
		$query = "select count(*), filename, uploadTime from little_main group by filename order by uploadTime desc";
		DB::query($query);
		
		$filename = array();
		$uploadTime = array();
		$count = array();
		
		while($row = DB::getResult()->fetch_assoc()){
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
			array_push($count, $row['count(*)']);
		}

		$smarty->assign(array(
					'main_filename' => $filename,
					'main_uploadTime' => $uploadTime,
					'main_count' => $count
		));
		
		$query = "select count(*), filename, uploadTime from little_main3 group by filename order by uploadTime desc";
		DB::query($query);
		
		$filename = array();
		$uploadTime = array();
		$count = array();
		
		while($row = DB::getResult()->fetch_assoc()){
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
			array_push($count, $row['count(*)']);
		}

		$smarty->assign(array(
					'main_filename3' => $filename,
					'main_uploadTime3' => $uploadTime,
					'main_count3' => $count
		));
		
		$filename = array();
		$uploadTime = array();
		$count = array();
		
		$query = "select count(*), filename, uploadTime from little_ruku group by filename order by uploadTime desc";
		DB::query($query);
		while($row = DB::getResult()->fetch_assoc()){
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
			array_push($count, $row['count(*)']);
		}

		$smarty->assign(array(
					'ruku_filename' => $filename,
					'ruku_uploadTime' => $uploadTime,
					'ruku_count' => $count
		));
		
		$filename = array();
		$uploadTime = array();
		$count = array();
		
		$query = "select id,filename, uploadTime from little_chuku_file group by filename order by uploadTime desc";
		DB::query($query);
		$result = DB::getResult();
		while($row = $result->fetch_assoc()){
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
			$query = "select count(*) from little_chuku where fileId = {$row['id']} group by fileId";
			DB::query($query);
			$row = DB::getResult()->fetch_assoc();
			array_push($count, $row['count(*)']);
		}

		$smarty->assign(array(
					'chuku_filename' => $filename,
					'chuku_uploadTime' => $uploadTime,
					'chuku_count' => $count
		));
		
		$smarty->display("listAllFiles.html");
		
	}else{
		$_GET['keyname'] = trim($_GET['keyname']);
		
		$query = "select *
			from little_main where filename like '%{$_GET['keyname']}%' order by filename, certificationNumber";
		DB::query($query);
		
		
		$fileId = null;
	
	$certificationNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$facheNumber = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);			
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($materialNumber, $row['materialNumber']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($sampleBatch,$row['sampleBatch']);
			array_push($facheNumber,$row['facheNumber']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($shipmentDate,$row['shipmentDate']);
			array_push($month,$row['month']);
			array_push($vehicleNumber, $row['vehicleNumber']);
			array_push($consignee, $row['consignee']);
			array_push($remark, $row['remark']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array('main_certificationNumber'	=> 	$certificationNumber,
				'main_certificationCodification'		=> 	$certificationCodification,
				'main_sequenceNumber'		=> 	$sequenceNumber,
				'main_manufactory'		=> 	$manufactory,
				'main_authenticationMark'		=> 	$authenticationMark,
				'main_steelNumber'		=> 	$steelNumber,
				'main_size1'		=> 	$size1,
				'main_size2'		=> 	$size2,
				'main_size3'		=> 	$size3,
				'main_count'	=>	$count,
				'main_unitWeight'		=>	$unitWeight,
				'main_orderNumber'		=>	$orderNumber,
				'main_orderSubitemNumber'	=>	$orderSubitemNumber,
				'main_unitPrice'		=>	$unitPrice,
				'main_materialNumber'		=>  $materialNumber,
				'main_consignmentBatch'	=>  $consignmentBatch,
				'main_sampleBatch'		=>  $sampleBatch,
				'main_facheNumber'	=>  $facheNumber,
				'main_purchaseNumber'	=>  $purchaseNumber,
				'main_shipmentDate'	=>  $shipmentDate,
				'main_month'	=>  $month,
				'main_vehicleNumber'	=>  $vehicleNumber,
				'main_consignee'	=>  $consignee,
				'main_remark'	=>  $remark,
				'main_filename'	=>  $filename,
				'main_uploadTime'	=>  $uploadTime
				));
				
	$query = "select *
	from little_main3 where filename like '%{$_GET['keyname']}%' order by filename, materialCode";
	DB::query($query);

	$materialCode = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();

	$purchaseNumber = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($materialCode, $row['materialCode']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);			
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($remark, $row['remark']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array('main3_materialCode'	=> 	$materialCode,
				'main3_sequenceNumber'		=> 	$sequenceNumber,
				'main3_manufactory'		=> 	$manufactory,
				'main3_authenticationMark'		=> 	$authenticationMark,
				'main3_steelNumber'		=> 	$steelNumber,
				'main3_size1'		=> 	$size1,
				'main3_size2'		=> 	$size2,
				'main3_size3'		=> 	$size3,
				'main3_count'	=>	$count,
				'main3_unitWeight'		=>	$unitWeight,
				'main3_orderNumber'		=>	$orderNumber,
				'main3_orderSubitemNumber'	=>	$orderSubitemNumber,
				'main3_unitPrice'		=>	$unitPrice,
				'main3_purchaseNumber'	=>  $purchaseNumber,
				'main3_remark'	=>  $remark,
				'main3_filename'	=>  $filename,
				'main3_uploadTime'	=>  $uploadTime
				));
				
		$query = "select *
			from little_ruku where filename like '%{$_GET['keyname']}%' order by filename, certificationNumber";
		DB::query($query);
		$rukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($rukuDate, $row['rukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array('ruku_rukuDate' => $rukuDate,
					'ruku_certificationNumber' => $certificationNumber,
					'ruku_certificationCodification' => $certificationCodification,
					'ruku_consignmentBatch' => $consignmentBatch,
					'ruku_remark1' => $remark1,
					'ruku_remark2' => $remark2,
					'ruku_filename' => $filename,
					'ruku_uploadTime' => $uploadTime
				));
				
		$query = "select * from 
				(
					(select * from little_chuku where fileId in (select id from little_chuku_file where filename like '%{$_GET['keyname']}%')) as chukuTable
				  		left join
				  	(select id,filename,uploadTime from little_chuku_file) as chukuFileTable
				  	on (chukuFileTable.id = chukuTable.fileId)
				 )
				 order by certificationNumber";
		DB::query($query);
		
		$chukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$handler = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($chukuDate, $row['chukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($handler, $row['handler']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$smarty->assign(array('chuku_chukuDate' => $chukuDate,
					'chuku_certificationNumber' => $certificationNumber,
					'chuku_certificationCodification' => $certificationCodification,
					'chuku_handler' => $handler,
					'chuku_consignmentBatch' => $consignmentBatch,
					'chuku_remark1' => $remark1,
					'chuku_remark2' => $remark2,
					'chuku_filename' => $filename,
					'chuku_uploadTime' => $uploadTime
				));
		$smarty->display('simpleTable.html');
	}
	
}

}
catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}
?>